package cn.fulong.sso.client.utils;


import cn.fulong.sso.client.manager.SessionManager;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;

/**
 * @desc: 将对Spring容器的依赖，降级到此处，可以方便切换实例的创建方式，减少其它类的修改
 * @author: 云树
 * @createDate: 2019/1/17 10:48
 * @version: 1.0
 */
public class SessionManagerUtils {

    /**
     * @param servletContext
     * @return spring容器内的SessionManager
     */
    public static SessionManager getRegisterSessionMangerBean(ServletContext servletContext) {
        //获取SSo服务端全局配置
        return geRegistertBean(servletContext, SessionManager.class);
    }

    public static <T> T geRegistertBean(ServletContext servletContext, Class<T> requiredType) throws BeansException {
        ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        return applicationContext.getBean(requiredType);
    }

}
